package main.Q601_700;

public class Q601_610 {
    public static void main(String[] args) {
        System.out.println("Question601：");
        System.out.println("Question602：");
        System.out.println("Question603：");
        System.out.println("Question604：");
        System.out.println("Question605：种花问题");
        System.out.println("Question606：根据二叉树创建字符串");
        TreeNode node1=new TreeNode(1);
        TreeNode node2=new TreeNode(2);
        TreeNode node3=new TreeNode(3);
        TreeNode node4=new TreeNode(4);
        node1.left=node2;
        node1.right=node3;
        node2.right=node4;
        Question606 temp=new Question606();
        System.out.println(temp.tree2str(node1));
        System.out.println("Question607：");
        System.out.println("Question608：");
        System.out.println("Question609：");
        System.out.println("Question610：");
    }
}

class Question605{
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        for (int i = 0, len = flowerbed.length; i < len && n > 0;) {
            if (flowerbed[i] == 1) i += 2;
            else if (i == flowerbed.length - 1 || flowerbed[i + 1] == 0) {
                n--;
                i += 2;
            } else i += 3;
        }
        return n <= 0;
    }
}

class Question606{
    public void process(StringBuffer str,TreeNode root){
        if (root==null) return;
        if (root.left==null&&root.right==null) {//叶子节点
            str.append(root.val);
            return;
        }
        str.append(root.val);
        str.append('(');
        process(str,root.left);
        str.append(')');
        if (root.right!=null){
            str.append('(');
            process(str,root.right);
            str.append(')');
        }
    }
    public String tree2str(TreeNode root) {
        StringBuffer str=new StringBuffer();
        process(str,root);
        return str.toString();
    }
}